const {
    generateMid,
    generateUuid,
    generateDeviceId,
    generateSign,
    transCookies
} = require('../src/utils/xianyuUtils');

describe('闲鱼工具函数测试', () => {
    
    describe('generateMid', () => {
        test('应该生成有效的消息ID', () => {
            const mid = generateMid();
            expect(mid).toBeDefined();
            expect(typeof mid).toBe('string');
            expect(mid.length).toBeGreaterThan(10);
        });
        
        test('每次生成的ID应该不同', () => {
            const mid1 = generateMid();
            const mid2 = generateMid();
            expect(mid1).not.toBe(mid2);
        });
    });
    
    describe('generateUuid', () => {
        test('应该生成有效的UUID', () => {
            const uuid = generateUuid();
            expect(uuid).toBeDefined();
            expect(typeof uuid).toBe('string');
            expect(uuid.startsWith('-')).toBe(true);
        });
        
        test('每次生成的UUID应该不同', () => {
            const uuid1 = generateUuid();
            const uuid2 = generateUuid();
            expect(uuid1).not.toBe(uuid2);
        });
    });
    
    describe('generateDeviceId', () => {
        test('应该生成有效的设备ID', () => {
            const userId = '123456789';
            const deviceId = generateDeviceId(userId);
            
            expect(deviceId).toBeDefined();
            expect(typeof deviceId).toBe('string');
            expect(deviceId.endsWith(`-${userId}`)).toBe(true);
            expect(deviceId.length).toBeGreaterThan(36);
        });
        
        test('应该包含正确的UUID格式', () => {
            const userId = '987654321';
            const deviceId = generateDeviceId(userId);
            
            // UUID格式: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
            const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}-\d+$/i;
            expect(deviceId).toMatch(uuidPattern);
        });
    });
    
    describe('generateSign', () => {
        test('应该生成有效的MD5签名', () => {
            const t = '1234567890';
            const token = 'test_token_123';
            const data = '{"test": "data"}';
            
            const sign = generateSign(t, token, data);
            
            expect(sign).toBeDefined();
            expect(typeof sign).toBe('string');
            expect(sign.length).toBe(32); // MD5 hash length
            expect(/^[a-f0-9]{32}$/i.test(sign)).toBe(true);
        });
        
        test('相同参数应该生成相同签名', () => {
            const t = '1234567890';
            const token = 'test_token_123';
            const data = '{"test": "data"}';
            
            const sign1 = generateSign(t, token, data);
            const sign2 = generateSign(t, token, data);
            
            expect(sign1).toBe(sign2);
        });
        
        test('不同参数应该生成不同签名', () => {
            const t = '1234567890';
            const token = 'test_token_123';
            const data1 = '{"test": "data1"}';
            const data2 = '{"test": "data2"}';
            
            const sign1 = generateSign(t, token, data1);
            const sign2 = generateSign(t, token, data2);
            
            expect(sign1).not.toBe(sign2);
        });
    });
    
    describe('transCookies', () => {
        test('应该正确解析Cookie字符串', () => {
            const cookiesStr = 'session=abc123; user=john; token=xyz789';
            const cookies = transCookies(cookiesStr);
            
            expect(cookies).toEqual({
                session: 'abc123',
                user: 'john',
                token: 'xyz789'
            });
        });
        
        test('应该处理包含等号的Cookie值', () => {
            const cookiesStr = 'session=abc=123; user=john=doe';
            const cookies = transCookies(cookiesStr);
            
            expect(cookies).toEqual({
                session: 'abc=123',
                user: 'john=doe'
            });
        });
        
        test('应该处理空Cookie字符串', () => {
            const cookiesStr = '';
            const cookies = transCookies(cookiesStr);
            
            expect(cookies).toEqual({});
        });
        
        test('应该处理单个Cookie', () => {
            const cookiesStr = 'session=abc123';
            const cookies = transCookies(cookiesStr);
            
            expect(cookies).toEqual({
                session: 'abc123'
            });
        });
    });
});
